package com.example.livedatautilsdemo.viewmodel;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;

import com.example.livedatautilsdemo.helper.LiveDataHelper;
import com.example.livedatautilsdemo.helper.LiveDataTimeHelper;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author: njb
 * @date: 2023/8/2 23:40
 * @desc:
 */
public class TimerLiveDataViewModel extends ViewModel {
    private LiveDataTimeHelper liveDataHelper;

    public LiveDataTimeHelper getCurrentSecondLiveData() {
        if (liveDataHelper == null) {
            liveDataHelper = new LiveDataTimeHelper();
        }
        return liveDataHelper;
    }

    public void startTiming(int seconds) {
        if (liveDataHelper != null) {
            liveDataHelper.startCountDown(seconds * 1000, 1000);
        }
    }

    public void stopTiming() {
        if (liveDataHelper != null) {
            liveDataHelper.stopCountDown();
        }
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        stopTiming();
    }
}
